package com.ocpap.rest.controller;

import com.alibaba.fastjson.JSONObject;
import com.ocpap.rest.entity.GoodsDict;
import com.ocpap.rest.service.WordDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("goodsdict")
public class WordDictController {

    @Autowired
    private WordDictService wordDictService;

    @RequestMapping("list")
    public String list(GoodsDict goodsDict,Model model){

        List<GoodsDict> goodsDicts = wordDictService.findAll(goodsDict);
        model.addAttribute("goodDicts",goodsDicts);
        return "/goodsdictlist";
    }

    @RequestMapping("add")
    public String add(GoodsDict goodsDict,Model model){
        return "/goodsdictadd";
    }

    @RequestMapping("update")
    public String update(Integer id,Model model){

        GoodsDict goodsDict1 = wordDictService.findById(id);
        model.addAttribute("goodsDict",goodsDict1);
        return "/goodsdictupdate";
    }

    @RequestMapping("insert")
    @ResponseBody
    public JSONObject insert(GoodsDict goods){

        JSONObject json = new JSONObject();
        int result = wordDictService.insert(goods);
        if(result>0){
            json.put("status","succ");
            json.put("msg","添加成功");
        }else{
            json.put("status","error");
            json.put("msg","添加失败");
        }
        return json;
    }

    @RequestMapping("mod")
    @ResponseBody
    public JSONObject mod(GoodsDict goods){

        JSONObject json = new JSONObject();
        int result = wordDictService.update(goods);
        if(result>0){
            json.put("status","succ");
            json.put("msg","更新成功");
        }else{
            json.put("status","error");
            json.put("msg","更新失败");
        }
        return json;
    }
}
